package com.shell.customer.invitation.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.customer.group.service.CusGrpService;
import com.shell.customer.invitation.service.InvtCusService;
import com.shell.customer.invitation.vo.Invitation;
import com.shell.customer.po.CusGrp;
import com.shell.customer.po.CusInvt;
import com.shell.feign.PubUserExtraFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.CommonFileUtil;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InvtCusController extends BaseController {
	@Autowired
	private InvtCusService invtCusService;
	@Autowired
	private CusGrpService cusGrpService;
	@Autowired
	private PubUserExtraFeign pubUserExtraFeign;
	protected final Log logger = LogFactory.getLog(getClass());

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg editInvitation(CusInvt invitation, String impType) throws Exception {
		this.invtCusService.editInvitation(invitation);
		return initRetMsg("success", "��������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg impt(String fileId, String inviteeCusType, String inviterId) throws Exception {
		String msg = this.invtCusService.impt(fileId, inviteeCusType, inviterId);
		if (StringUtil.notEmpty(msg)) {
			msg = CommonFileUtil.changMsg(msg);
			return new RetMsg("error", false, msg);
		}
		return initRetMsg("success", "��������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg invite(String invtIds) throws Exception {
		this.invtCusService.invite(invtIds);
		String[] invtIdArr = invtIds.split(",");
		String tip = "";
		if (invtIdArr.length > 1) {
			tip = invtIdArr.length + "��";
		}
		return initRetMsg("success", "��������" + tip + "����������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public List<Invitation> findInvitations(PageLimit limit, Invitation invitation) {
		invitation.setInviteeCusType("MEMBER");
		if (StringUtil.empty(invitation.getInviterId())) {
			invitation.setInviterId(CommonDataUtil.getCurEntId());
		}
		return this.invtCusService.findInvitations(limit, invitation);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public List<Invitation> findInvitationsForGroup(PageLimit limit, Invitation invitation) {
		if (StringUtil.empty(invitation.getInviterId())) {
			List<CusGrp> grps = this.cusGrpService.selectCusGroupByTagCode(CommonDataUtil.getCurEntId(), "GROUP_ADMIN");
			List<String> grpIds = new ArrayList();
			for (CusGrp grp : grps) {
				grpIds.add(grp.getGrpId());
			}
			invitation.setInviterIds((String[]) grpIds.toArray(new String[grpIds.size()]));
		}
		return this.invtCusService.findInvitationsForGroup(limit, invitation);
	}

	@RequestMapping
	@Logging(module = "����������", methods = "��������")
	public List<Invitation> findSuppliers(PageLimit limit, Invitation invitation) {
		invitation.setInviterId(CommonDataUtil.getCurEntId());
		invitation.setInviteeCusType("SUPPLIER");
		return this.invtCusService.findInvitations(limit, invitation);
	}

	@RequestMapping
	@Logging(module = "����������", methods = "����������")
	public Map<String, String> getInviteCode() throws Exception {
		Map<String, String> m = new HashMap();
		String userId = CommonDataUtil.getUserId();
		String entId = CommonDataUtil.getCurEntId();
		m.put("inviteCode", this.pubUserExtraFeign.getInviteCode(userId, entId));
		return m;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public void downloadTemplate(String type, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String pathValue = "";
		String fileName = "";
		if ("InviteMemberImport".equals(type)) {
			pathValue = "/template/excel-import/InviteSupplierImport.xlsx";
			fileName = "����������������.xlsx";
		} else if ("InviteCustomerImport".equals(type)) {
			pathValue = "/template/excel-import/InviteCustomerImport.xlsx";
			fileName = "������������.xlsx";
		} else if ("InviteSupplierImport".equals(type)) {
			pathValue = "/template/excel-import/InviteSupplierImport.xlsx";
			fileName = "��������������.xlsx";
		} else {
			return;
		}
		InputStream is = InvtCusController.class.getResourceAsStream(pathValue);
		CommonFileUtil.writeFileToClient(fileName, is, response, request);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "������������������������������")
	public List<Invitation> countInviteNum() {
		return this.invtCusService.countInviteNum(CommonDataUtil.getCurEntId());
	}
}
